import { reqCreateTeacher, reqDeleteTeacherById, reqDeleteUserById, reqGetTeacherListByApplyProject, reqGetUserMessageList, reqUpdateAccountRoleById, reqUpdateAccountStateById } from "@/api"
import { Message } from "element-ui";
export default({
    namespaced : true ,
    actions:{
        async getUserMessageListByPurview(context,value){
            let result=await reqGetUserMessageList();
            if(result.code==='200'){
                context.commit('getUserMessageListByPurview',result.other)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateUserAccountStateById(context,value){
            let result=await reqUpdateAccountStateById(value);
            if(result.code==='200'){
                context.commit('updateUserAccountStateById',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateAccountRoleById(context,value){
            let result=await reqUpdateAccountRoleById(value);
            if(result.code==='200'){
                context.commit('updateAccountRoleById',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteAccountById(context,value){
            let result=await reqDeleteUserById(value);
            if(result.code==='200'){
                context.commit('deleteAccountById',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async getTeacherList(context,value){
            let result=await reqGetTeacherListByApplyProject();
            if(result.code==='200'){
                context.commit('getTeacherList',result.other)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteTeacherById(context,value){
            let result=await reqDeleteTeacherById(value);
            if(result.code==='200'){
                context.commit('deleteTeacherById',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async createTeacher(context,value){
            let result=await reqCreateTeacher(value);
            if(result.code==='200'){
                context.commit('createTeacher',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        }
    },
    mutations:{
        getUserMessageListByPurview(state,value){
            state.userList=value.userList;
            state.roleList=value.roleList;
            state.authorityList=value.authorityList;
        },
        updateUserAccountStateById(state,value){
            if(value.type==='account'){
                let originArr=state.userList;
                originArr.forEach((element,index) => {
                    if(element.id===value.id){
                        let obj=element;
                        obj.state=value.state;
                        state.userList.splice(index,1)
                        state.userList.push(obj);
                    }
                });
            }
            if(value.type==='authority'){
                let originArr=state.authorityList;
                originArr.forEach((element,index)=>{
                    if(element.id===value.id){
                        let obj=element;
                        obj.state=value.state;
                        state.authorityList.splice(index,1);
                        state.authorityList.push(obj);
                    }
                })
            }
            
        },
        updateAccountRoleById(state,value){
            let originArr=state.userList;
            originArr.forEach((element,index)=>{
                if(element.id===value.id){
                    let obj=element;
                    obj.role=value.role;
                    state.userList.splice(index,1);
                    state.userList.push(obj);
                }
            })
        },
        deleteAccountById(state,value){
            let originArr=state.userList;
            originArr.forEach((element,index)=>{
                if(element.id===value.id){
                    state.userList.splice(index,1)
                }
            })
        },
        getTeacherList(state,value){
            state.teacherList=value;
        },
        deleteTeacherById(state,value){
            let originArr=state.teacherList;
            originArr.forEach((element,index)=>{
                if(element.id===value.id){
                    state.teacherList.splice(index,1)
                }
            })
        },
        createTeacher(state,value){
            state.teacherList.push(value)
        }
    },
    state:{
        userList:[],
        roleList:[],
        authorityList:[],
        teacherList:[]
    },
    getters:{
        
    }
})